﻿Imports System.IO

' Events for controls inside of the Mainform
Public Class ControlEvents

    ' Context Menu strips
    Private Property ProjectFileMenuStrip As New ProjectFileContextMenu()
    Private Property ProjectFolderMenuStrip As New ProjectFolderContextMenu()

    Public Sub FileView_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Dim selectednode = MainForm.FileView.SelectedNode
        If Not IsNothing(selectednode) Then
            If e.KeyCode = Keys.Enter Then
                Dim curproj = Globals.CurrentProject
                Dim fullpath As String = curproj.NodeFullPathToFullName(selectednode.FullPath)
                If curproj.FileList.IsProjectFile(fullpath) Then
                    Dim projectfile = curproj.FileList.GetProjectFile(fullpath)
                    MainForm.EditorTabManager.AddTab(projectfile.FileName, projectfile)
                ElseIf fullpath.EndsWith(curproj.ProjectName) Then
                    If selectednode.IsExpanded Then
                        selectednode.Collapse()
                    Else
                        selectednode.Expand()
                    End If

                ElseIf curproj.FileList.IsProjectFolder(fullpath) Then
                    If selectednode.IsExpanded Then
                        selectednode.Collapse()
                    Else
                        selectednode.ExpandAll()
                    End If
                End If
            End If
        End If
    End Sub

    Public Sub FileView_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim node As TreeNode = CType(MainForm.FileView.GetNodeAt(e.Location), TreeNode)
        MainForm.FileView.SelectedNode = node
        Dim fullpath As String = Globals.CurrentProject.NodeFullPathToFullName(node.FullPath)
        fullpath = fullpath.Replace(Globals.CurrentProject.ProjectName + "\", "")

        Dim curproj = Globals.CurrentProject

        If e.Button = Windows.Forms.MouseButtons.Left Then
            If MainForm.FileView.FileViewType <> FileView.FileViewMode.CLASSIC Then
                ' Expand the Folder
                If Not curproj.FileList.IsProjectFile(fullpath) And
                    Not fullpath.EndsWith(curproj.ProjectName) Then
                    If node.IsExpanded Then
                        node.Collapse()
                    Else
                        node.ExpandAll()
                    End If
                End If
            End If
        ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
            ' Open the context menu
            If curproj.FileList.IsProjectFile(fullpath) Then
                ProjectFileMenuStrip.ClickedProjectFile =
                    Globals.CurrentProject.FileList.GetProjectFile(fullpath)

                Dim location = New Drawing.Point(e.Location.X,
                         e.Location.Y + CType(ProjectFileMenuStrip.Height / 2, Integer) +
                         CType(ProjectFileMenuStrip.Items(0).Height * 1.5, Integer))

                ProjectFileMenuStrip.Show(location)
            ElseIf curproj.FileList.IsProjectFolder(fullpath) Then
                ' Do a project folder context strip
                ProjectFolderMenuStrip.ClickedProjectFolder =
                    Globals.CurrentProject.FileList.GetProjectFolder(fullpath)

                Dim location = New Drawing.Point(e.Location.X,
                         e.Location.Y + CType(ProjectFileMenuStrip.Height / 2, Integer) +
                         CType(ProjectFileMenuStrip.Items(0).Height * 1.5, Integer))

                ProjectFolderMenuStrip.Show(location)
            Else
                ' This is the actual project node
            End If
        End If
    End Sub

    Public Sub FileView_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs)
        Dim projectfile As New Project.ProjectFile(Globals.CurrentProject.DevelopmentFolder +
                    MainForm.FileView.SelectedNode.FullPath.Replace(Globals.CurrentProject.ProjectName + "\", ""))
        Dim fileinfo As New FileInfo(projectfile.FullName)
        If fileinfo.Exists() And Not fileinfo.Attributes = FileAttributes.Directory Then
            MainForm.EditorTabManager.AddTab(projectfile.FileName, projectfile)
        End If
    End Sub

    Public Sub EditorTabs_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.Modifiers.HasFlag(Keys.Control) Then
            If e.KeyCode = Keys.W Then
                MainForm.EditorTabManager.CloseCurrentTab()
            End If
        End If
    End Sub

    Public Sub EditorTabs_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

        MainForm.EditorTabManager.TabClicked = Nothing
        For i As Integer = 0 To MainForm.EditorTabs.TabCount - 1
            If MainForm.EditorTabs.GetTabRect(i).Contains(e.Location) Then
                MainForm.EditorTabManager.TabClicked = MainForm.EditorTabs.TabPages(i)
                Exit For
            End If
        Next

        ' Exit Tabs on Middle Click
        If e.Button = Windows.Forms.MouseButtons.Middle Then
            MainForm.EditorTabManager.CloseTab(MainForm.EditorTabManager.TabClicked.Name)
        ElseIf e.Button = MouseButtons.Right Then
            MainForm.EditorTabMenuStrip.Show(MainForm.EditorTabs, e.Location)
        End If
    End Sub
End Class
